import os
from wxpy import *
import threading


class Itchat(object):

    def __init__(self):
        self.bot = None

    def login(self):
        th = threading.Thread(target=self.creatBot)
        th.start()

    def creatBot(self):
        self.bot = Bot(qr_path=os.path.join(sys.path[0] + '/static/', 'qrcode.jpg'), cache_path=True,
                       login_callback=self.loginSuccess, qr_callback=self.qrSuccess)

    def loginSuccess(self):
        print('登录成功')

    def qrSuccess(self, uuid, status, qrcode):
        with open(os.path.join(sys.path[0] + '/static/', 'qrcode.jpg'), 'wb') as f:
            f.write(qrcode)
            f.close()

    @property
    def myself(self):
        if self.bot:
            return self.bot.friends(update=True)[0]
        else:
            return None


mychat = Itchat()
